import path from "node:path";
import { readdir } from "node:fs/promises";
import { getFileNameFromUrl } from "../utils/index.js";

export const registRouter = async (app) => {
  const routerModule = path.join(__dirname, "modules");
  const files = await readdir(routerModule);
  files.forEach(async (item) => {
    const routerFilePath = path.join(routerModule, item);
    const fileName = getFileNameFromUrl(routerFilePath);
    const file = (await import(routerFilePath)).default;
    if(fileName==="base"){
      app.use('/', file);
    }else{
      app.use('/'+fileName, file);
    }
  });
};

